package task3

import grails.test.mixin.Mock
import grails.test.mixin.TestFor

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(OrganizationService)
@Mock([Department, Project, Human])
class OrganizationServiceTests {

    void testGetNodeInDataBase() {
        def dep = new Department(name: "Отдел1")
        dep.save()
        def proj = new Project(name: "Проект1", department: dep)
        proj.save()
        def hum = new Human(name: "Sun Wain", position: "tester", project: proj)
        hum.save()

        def params = [:]

        params.id = service.HUMAN_ID_PREFIX + hum.getId()
        def objHum = service.getNodeInDatabase(params)

        assertEquals objHum, hum

        params.id = service.getPROJECT_ID_PREFIX() + proj.getId()
        def objProj = service.getNodeInDatabase(params)

        assertEquals objProj, proj

        params.id = service.DEPARTMENT_ID_PREFIX + dep.getId()
        def objDep = service.getNodeInDatabase(params)

        assertEquals objDep, dep

        params.id = service.ORGANIZATION_ID_PREFIX + 1
        def objNull = service.getNodeInDatabase(params)

        assertNull objNull
    }

    void testSaveDomains() {
        def dep = new Department(name: "")
        dep.save()

        assertEquals 0, Department.list().size()

        def paramsDep = [:]
        paramsDep.name = "Отдел1"

        service.saveDepartment(dep, paramsDep)

        assertEquals 1, Department.list().size()
        assertEquals "Отдел1",dep.name

        def proj = new Project(name: "", department: dep)
        proj.save()

        assertEquals 0, Project.list().size()

        def paramsProj = [:]
        paramsProj.name="Проект1"

        service.saveProject(proj, paramsProj)
        assertEquals 1, Project.list().size()
        assertEquals "Проект1",proj.name

        def hum = new Human(name: "Sun Wain", position: "",project: proj)
        hum.save()

        def paramsHum = [:]
        paramsHum.position="tester"
        assertEquals 0, Human.list().size()

        service.saveHuman(hum, paramsHum)
        assertEquals 0, Human.list().size()

        paramsHum.name="Petr"
        service.saveHuman(hum, paramsHum)
        assertEquals 1, Human.list().size()
        assertEquals "tester",hum.position
        assertEquals "Petr",hum.name
    }

    void testGetErrorsMessage(){
        def dep = new Department(name: "")

        assertFalse dep.validate()
        def errorDepMap=service.getErrorMessage(dep)
        assertEquals errorDepMap.name,"blank"

        def proj = new Project()

        assertFalse proj.validate()
        def errorProjMap=service.getErrorMessage(proj)
        assertEquals errorProjMap.name,"nullable"

        def human =new Human(name:"",age:154,humanEmail: "noEmail")

        assertFalse human.validate()
        def errorHumMap=service.getErrorMessage(human)
        assertEquals errorHumMap.name, "blank"
        assertEquals errorHumMap.position, "nullable"
        assertEquals errorHumMap.age, "max.exceeded"
        assertEquals errorHumMap.humanEmail, "email.invalid"
    }

    void testUpdateNode(){
        def dep = new Department(name: "Отдел1")
        dep.save()
        def proj = new Project(name: "Проект1", department: dep)
        proj.save()
        def hum = new Human(name: "Sun Wain", position: "tester", project: proj)
        hum.save()


        def depFound =Department.get(dep.id)
        assertEquals depFound.name,"Отдел1"

        def paramsDep = [:]
        paramsDep.id=service.DEPARTMENT_ID_PREFIX+dep.id
        paramsDep.name = "Отдел666"


        def resultMap =service.updateNode(paramsDep)
        assertEquals resultMap.success,true
        assertEquals resultMap.msg,service.getSuccessMessage()

        depFound =Department.get(dep.id)
        assertEquals depFound.name,"Отдел666"


        paramsDep.name = "1"
        resultMap =service.updateNode(paramsDep)
        assertEquals resultMap.success,false
        assertEquals resultMap.msg,service.getFailureMessage()

        depFound =Department.get(dep.id)
        assertEquals depFound.name,"Отдел666"
        assertEquals 1, Department.list().size()


    }

    void testSaveNewNode(){
        def dep = new Department(name: "Отдел1")
        dep.save()

        def paramsProj = [:]
        paramsProj.name="Проект1"
        paramsProj.parentId=service.DEPARTMENT_ID_PREFIX+dep.id

        def resultMap = service.saveNewNode(paramsProj)
        assertEquals resultMap.success,true

        paramsProj.name="G"
        paramsProj.parentId=service.DEPARTMENT_ID_PREFIX+dep.id
        resultMap = service.saveNewNode(paramsProj)
        assertEquals resultMap.success,false
        assertEquals resultMap.errors.name, "size.toosmall"
    }
}
